package com.latefly.skewwork.ui.page.tools.todo

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.latefly.skewwork.data.dto.vo.TodoItem

class TodoViewModel : ViewModel() {
    //内部属性只读
    var todoItems = mutableStateListOf<TodoItem>()
        private set

    private var currentEditPosition by mutableStateOf(-1)
    val currentEditItem: TodoItem?
        get() = todoItems.getOrNull(currentEditPosition)

    fun addItem(item: TodoItem) {
        todoItems.add(item)
        currentEditPosition = -1
    }

    fun removeItem(item: TodoItem) {
        todoItems.remove(item)
        currentEditPosition = -1
    }

    fun onEditDone(item: TodoItem) {
        currentEditPosition = -1
    }

    fun onEditItemSelected(item: TodoItem) {
        currentEditPosition = todoItems.indexOf(item)
    }

    fun onEditItemChange(item: TodoItem) {
        val currentItem = requireNotNull(item) {

        }
        todoItems[currentEditPosition] = item
    }
}